package com.inspur.dgov.vlbod.secure.config;

import com.inspur.dgov.vlbod.secure.interceptor.SecureInterceptor;
import com.inspur.dgov.vlbod.secure.props.DgovSecureProperties;
import com.inspur.dgov.vlbod.secure.registry.SecureRegistry;
import lombok.AllArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 安全配置类
 *
 * @Author chiyupei
 * @Date 19/6/27 下午4:14
 * @Version 1.0
 * @Description 安全配置类
 */
@Order
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties({DgovSecureProperties.class})
public class SecureConfig implements WebMvcConfigurer {

    private final SecureRegistry secureRegistry;

    private final DgovSecureProperties secureProperties;

    @Bean
    public SecureInterceptor secureInterceptor() {
        return new SecureInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (secureRegistry.isEnable()) {
            registry.addInterceptor(secureInterceptor())
                    .excludePathPatterns(secureRegistry.getExcludePatterns())
                    .excludePathPatterns(secureRegistry.getDefaultExcludePatterns())
                    .excludePathPatterns(secureProperties.getExcludePatterns());
        }
    }
}
